<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
          <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol"  label="资产管理员" prop="inventor">
            <j-search-select-tag 
              placeholder="请选择资产管理员" 
              v-model="model.inventor" 
              :dictOptions="searchOptions"
              @change="(v)=>handleChoose(v,model)"
            >
            </j-search-select-tag>
          </a-form-model-item>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction, postAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import { mapActions, mapGetters, mapState } from 'vuex'

export default {
  name: 'AssetPersonForm',

  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
        searchOptions:[],
        selectName:"",
        selectDeptId:"",
        model:{
         },
         labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/test/ceshiNote/add",
          edit: "/assets/deptExt/edit",
          queryById: "/test/ceshiNote/queryById",
          queryListByDepartId: "/sys/user/queryListByDepartId",
        }
      }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
   //备份model原始值
   this.modelDefault = JSON.parse(JSON.stringify(this.model));
   
  },
  
  methods: {
      deptListHandle(data){
        const that = this
        console.log('333')
        console.log(data)
        let formData = {
          deptId: data.deptId,
          pageSize: 200
        }
        getAction(this.url.queryListByDepartId,formData).then((res) => {
          console.log('33334')
          console.log(res)
          if(res.success){
            this.searchOptions = res.result.records.map(item=>{
                return{
                  ...item,
                  text:item.lastname,
                  value:item.lastname
                }
            })
            console.log('999')
            console.log(this.searchOptions)
          }
        }).finally(() => {
          that.confirmLoading = false;
        })
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.model.inventor = this.model.assetsUserName
        this.visible = true;
        this.deptListHandle(this.model)
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            const httpurl = this.url.edit
            const params = {
              assetsUserId:this.selectDeptId?this.selectDeptId:this.model.assetsUserId,
              assetsUserName:this.selectName?this.selectName:this.model.assetsUserName,
              deptId:this.model.deptId,
              id:this.model.id
            }
            httpAction(httpurl,params,'post').then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      handleChoose(v,model){
        this.selectName = v
        this.selectDeptId = this.searchOptions.filter(a => a.lastname == v)[0].id
      }
    }
  }
</script>
